/**
 * All Rights Reserved , Copyright (C) 2023 , 青岛鼎信通讯股份有限公司
 *
 *MeetPersonRecordHistoryController
 * 会议人员记录历史
 *
 * 修改纪录
 * 2023-03-30 版本：1.0 wangxinhua 创建。
 * @version 版本：1.0
 * @author 作者：wangxinhua
 * 创建日期：2023-03-30
 */

package com.topscomm.meet.controller;

import java.util.*;
import java.util.stream.Collectors;

import com.topscomm.basic.util.SQLAdapter;
import com.topscomm.meet.pojo.MeetManageEntity;
import com.topscomm.meet.pojo.MeetManageHistoryEntity;
import com.topscomm.meet.pojo.MeetPersonRecordEntity;
import com.topscomm.meet.pojo.MeetRecordEntity;
import com.topscomm.meet.service.IMeetManageHistoryService;
import com.topscomm.meet.service.IMeetManageService;
import com.topscomm.meet.service.IMeetRecordService;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.meet.controller.auto.MeetPersonRecordHistoryControllerAuto;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: wangxinhua
 * @date: 2023-03-30
 * @description:会议人员记录历史Controller
 * @modify:
 */
@RestController
@RequestMapping("meet/personRecordHistory")
public class MeetPersonRecordHistoryController extends MeetPersonRecordHistoryControllerAuto implements IMeetPersonRecordHistoryController {
	@Autowired
	private IMeetRecordService meetRecordService;
	@Autowired
	private IMeetManageService meetManageService;
	@Autowired
	private IMeetManageHistoryService meetManageHistoryService;
	/**
	 * @author: wangxinhua
	 * @date: 2023-03-30
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_meetid")) {
			String value = paraMap.get("qry_meetid").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and meethistoryid = '{0}'", value);
		}
		if (paraMap.containsKey("qry_submitstate")) {
			String value = paraMap.get("qry_submitstate").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and submitstate = {0}", value);
		}
		if (paraMap.containsKey("qry_signstate")) {
			String value = paraMap.get("qry_signstate").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and signstate = {0}", value);
		}
		return sql.toString();
	}

	/**
	 * @author: wangxinhua
	 * @date: 2023-03-30
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	@Override
	@GetMapping(value = "/list")
	public ResponseResult<Object> list(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			String sidx = ConvertUtil.convertToString(paraMap.get("sidx"));
			String usercode = MeetPersonRecordEntity.FieldUsercode;
			// 当按照员工工号排序时，先按照工号长度排序
			if (!StringUtil.isEmpty(sidx) && usercode.equals(sidx.replace(" ", ""))) {
				paraMap.put("sidx", SQLAdapter.len(usercode) + " asc, " + usercode);
			}
			Query query = new Query(paraMap);
			String sqlWhere = this.spellListSql(paraMap);
			Page page = new Page(new ArrayList<>(), 0, query.getPageSize(), query.getCurrentPage());
			// 补录会议纪要查询参会人员，不存在会议
			if (paraMap.containsKey("supplement")) {
				StringBufferProxy sql = new StringBufferProxy();
				MeetRecordEntity record = this.meetRecordService.queryById(ConvertUtil.convertToString(paraMap.get("qry_recordid")));
				MeetManageEntity meetManageEntity = meetManageService.queryById(ConvertUtil.convertToString(record.getMeetid()));
				//公司级远程有值，且不是创建人跟秘书
				String userMeetCode = meetManageEntity.getUsercode();
				String userManageCodes = "," + meetManageEntity.getManageusercodes() + ",";
				boolean viewHistory = false;
				if (meetManageEntity.getRemoteroomid() != 0 && !userMeetCode.equals(getUserBean().getUser().getCode()) && !userManageCodes.contains(getUserBean().getUser().getCode())) {
					viewHistory = true;
				}
				MeetManageHistoryEntity meetManageHistoryEntity = meetManageHistoryService.queryObjectByWhere(" meetid="+record.getMeetid() + " and valid=1");
				if (meetManageHistoryEntity != null && viewHistory) {
					sql.appendSingle(" meethistoryid = {0}", meetManageHistoryEntity.getId());
					int totalCount = this.service.getCount(sqlWhere);
					List<Map<String, Object>> tempList = null;
					if (query.getPageSize() > 0) {
						tempList = this.service.queryMapForPage(sqlWhere, query.getCurrentPage(), query.getPageSize(),
								query.getSidx(), query.getSord());
					} else {
						tempList = this.service.queryMapByWhere(sqlWhere);
					}
					page = new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
				} else {
					if (!StringUtil.isEmpty(record.getAbsentusercodes()) || !StringUtil.isEmpty(record.getAttendusercodes())) {
						String usercodes = (StringUtil.isEmpty(record.getAttendusercodes()) ? "" : record.getAttendusercodes())
								+ "," + (StringUtil.isEmpty(record.getAbsentusercodes()) ? "" : record.getAbsentusercodes());
						String[] split = usercodes.split(",");
						List<String> usercodeList = Arrays.stream(split).filter(item -> !StringUtil.isEmpty(item))
								.distinct().collect(Collectors.toList());
						usercodes = usercodeList.stream().collect(Collectors.joining("','", "'", "'"));
						Map<String, Object> param = new HashMap<>();
						param.put("usercodes", usercodes);
						param.put("begincount", query.getCurrentPage() * query.getPageSize() - query.getPageSize());
						param.put("pagesize", query.getPageSize());
						List<Map<String, Object>> tempList = null;
						if (query.getPageSize() > 0) {
							tempList = this.service.queryMap(param, "queryUserInfoByCodeForPage");
						} else {
							tempList = this.service.queryMap(param, "queryUserInfoByCode");
						}
						page = new Page(tempList, usercodeList.size(), query.getPageSize(), query.getCurrentPage());
					}
				}
			} else { // 正常会议参会人员
				int totalCount = this.service.getCount(sqlWhere);
				List<Map<String, Object>> tempList = null;
				if (query.getPageSize() > 0) {
					tempList = this.service.queryMapForPage(sqlWhere, query.getCurrentPage(), query.getPageSize(),
							query.getSidx(), query.getSord());
				} else {
					tempList = this.service.queryMapByWhere(sqlWhere);
				}
				page = new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
			}
			return ResponseResult.ok(page);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}
}

